package com.winshare.saleAssistant.domain.dto.req;

import com.winshare.saleAssistant.constant.MaterialType;
import com.winshare.saleAssistant.exception.BusinessException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.validator.constraints.Range;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@ApiModel
public class MaterialCreateDTO {

    @NotBlank(message = "物料分类或物料名称不得为空！！")
    @ApiModelProperty(value = "物料分类/物料名称")
    private String name;

    @NotNull(message = "父id不能为空，默认为0")
    @ApiModelProperty(value = "父物料分类id/父物料Id，默认为0")
    private Integer parentId;

    @ApiModelProperty(value = "物料类型值：1,分类;2,文件")
    @Range(min = 1, max = 2, message = "物料类型值：1,分类;2,文件")
    private Integer type;

    @ApiModelProperty(value = "文件url")
    private String fileUrl;

    public MaterialCreateDTO() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public void validMaterial(){
        if(parentId == null || parentId == 0) {
            if(type.intValue() == MaterialType.file) {
                throw new BusinessException("物料类型错误,当父分类Id为0或空时，此时物料类型为：分类");
            }
        }
        if(type.intValue() == MaterialType.file) {
            if(StringUtils.isEmpty(fileUrl)) {
                throw new BusinessException("物料类型为文件时，其文件上传地址不能为空");
            }
        }
        if(type.intValue() == MaterialType.category) {
            if(!StringUtils.isEmpty(fileUrl)) {
                throw new BusinessException("物料类型为 分类 时，其文件上传地址必须为空");
            }
        }
    }
}
